package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.GiveALike;
import com.lzx.article.feign.ESClient;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.GiveALikeMapper;
import com.lzx.article.service.GiveALikeService;
import com.lzx.article.vo.User;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class GiveALikeServiceImpl extends ServiceImpl<GiveALikeMapper, GiveALike> implements GiveALikeService {
    @Resource
    private ArticlesMapper articlesMapper;

    @Resource
    private GiveALikeMapper giveALikeMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ESClient esClient;

    //点赞功能
    @GlobalTransactional
    @Override
    public R saveUserGiveALike(GiveALike giveALike) {
        LambdaQueryWrapper<GiveALike> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id和文章id查找点赞表是否有相应记录
        queryWrapper.eq(GiveALike::getArticleId, giveALike.getArticleId()).eq(GiveALike::getUserId, giveALike.getUserId());
        GiveALike vo = giveALikeMapper.selectOne(queryWrapper);
        if (vo != null) {
            vo.setUpdateTime(new Date());
            if (vo.getIsLike() == 0) {
                vo.setIsLike(1);
                giveALikeMapper.updateById(vo);
                //点赞，则文章点赞数+1
                LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Articles::getId, giveALike.getArticleId());
                Articles article = articlesMapper.selectOne(wrapper);
                article.setLikeCount(article.getLikeCount() + 1);
                articlesMapper.updateById(article);
                //es同步
                esClient.editArticle(Long.valueOf(giveALike.getArticleId()));
                return R.ok().message("点赞成功");
            } else {
                vo.setIsLike(0);
                giveALikeMapper.updateById(vo);
                //取消点赞，则文章点赞数-1
                LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Articles::getId, giveALike.getArticleId());
                Articles article = articlesMapper.selectOne(wrapper);
                article.setLikeCount(article.getLikeCount() - 1);
                articlesMapper.updateById(article);
                //es同步
                esClient.editArticle(Long.valueOf(giveALike.getArticleId()));
                return R.ok().message("取消点赞");
            }
        } else {
            giveALike.setCreateTime(new Date());
            giveALike.setIsLike(1);
            giveALikeMapper.insert(giveALike);
            //点赞，则文章点赞数+1
            LambdaQueryWrapper<Articles> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Articles::getId, giveALike.getArticleId());
            Articles article = articlesMapper.selectOne(wrapper);
            article.setLikeCount(article.getLikeCount() + 1);
            articlesMapper.updateById(article);
            //es同步
            esClient.editArticle(Long.valueOf(giveALike.getArticleId()));

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(article.getUserId());
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(giveALike.getUserId()));
            inform.setSender(giveALike.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.LIKEARTICLE.getType());
            inform.setMessage(article.getTitle());
            userClient.saveInform(inform);

            return R.ok().message("点赞成功");
        }
    }

    //查询当前用户的点赞记录
    @Override
    public R getUserGiveALikeList(int userId) {
        QueryWrapper<GiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<GiveALike> list = giveALikeMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }

}
